package com.sgb.kjwl.view.ui.fragment.pick;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppFragment;
import com.sgb.link.utils.PickCateType;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickNoticingBidBinding;
import com.sgb.kjwl.di.pick.DaggerPickFragComponent;
import com.sgb.kjwl.di.pick.PickFragModule;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeBidDetailActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickSearchActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickWinningNoticeAdapter;
import com.sgb.kjwl.view.widget.CommonCategoryFilterPopWindow;
import com.sgb.kjwl.view.widget.PickHallBidingPopWindow;
import com.sgb.kjwl.view.widget.pick.PickChooseLeftDialog2;
import com.sgb.kjwl.view.widget.pick.PickPopWindowView;
import com.sgb.kjwl.viewmodel.pick.PickNoticingModel;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 招采——大搜索——中标公告
 */
public class PickNoticingBidFragment extends AppFragment implements PickChooseLeftDialog2.DialogCallBackImpl{

    public static final String KEY_TYPE = "type";
    public static final String KEY_TOTAL = "total";
    private FragmentPickNoticingBidBinding binding;
    //搜索关键字
    private String searchName = "";
    private int total = 0;
    private int page = 1;
    private PickWinningNoticeAdapter adapter;

    @Inject
    PickNoticingModel viewModel;
    private boolean isMore = false;
    private PickSearchActivity pickSearchActivity;

    private PickHallBidingPopWindow pickSortPopWindow;
    private CommonCategoryFilterPopWindow categoryFilterPopWindow;
    private PickHallBidingPopWindow pickMoneyPopWindow;
    private PickHallBidingPopWindow pickTabLayoutPopWindow;
    private ThreeLevelAreaWindow mAddressPop;

    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    private List<PickSelectEntity> sortData = new ArrayList<>();
    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> categoryDatas = new ArrayList<>();
    private List<PickSelectEntity> tabTypeList = new ArrayList<>();
    private PickSelectEntity all;

    //排序类型
    private String sortType = "1";
    private String sortTypeName;
    //省
    private String areacode;
    //地区
    private String citysNos;
    private String goodsType;
    private String goodsTypeName;
    private String pickMoney;
    private String pickMoneyName;
    //类型
    private String typeNos;
    private int tabPosition = 0;
    private int tabTypePosition;
    private boolean isDialogData = false;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;
    private String areaName;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPickFragComponent.builder()
                .appComponent(appComponent)
                .pickFragModule(new PickFragModule(this))
                .build()
                .inject(this);
    }

    public static PickNoticingBidFragment newInstance(String type, int total) {
        PickNoticingBidFragment fragment = new PickNoticingBidFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        bundle.putInt(KEY_TOTAL, total);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_noticing_bid, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        searchName = (String) getArguments().get(KEY_TYPE);
        total = getArguments().getInt(KEY_TOTAL, 0);
        pickSearchActivity = (PickSearchActivity) getActivity();
        initView();
        initData();
    }

    private void initView() {
        initChooseDialog();

        pickSearchActivity.showTopBack(false);
        binding.empty.setVisibility(View.VISIBLE);
        binding.noticeList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        adapter = new PickWinningNoticeAdapter(getActivity(), null);
        binding.noticeList.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.noticeList.setAdapter(adapter);
        binding.noticeList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isMore = false;
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                isMore = true;
                page ++;
                getData();
            }
        });

        adapter.setOnItemClick(new AdapterOnItemClick<PreNoticeItemEntity>() {
            @Override
            public void onItemClick(PreNoticeItemEntity goodsInfoEntity3, int position) {
                //中标
                PickNoticeBidDetailActivity.start(getActivity(), goodsInfoEntity3.getTenderNo() + "");
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sort:
                showSortPopWindow();
                break;
            case R.id.tv_area:
                showProvincePopWindow();
                break;
            case R.id.tv_pick_money:
                showMoneyPopWindow();
                break;
            case R.id.tv_category:
                if (null != categoryDatas && categoryDatas.size() > 0) {
                    updateChooseTextStatus(binding.tvCategory, TextUtils.isEmpty(goodsType) ? -1 : -2, null);
                    categoryFilterPopWindow.showPopWindow1(binding.tvCategory);
                    if (!TextUtils.isEmpty(typeNos)) {
                        categoryFilterPopWindow.setData2(categoryDatas.get(tabTypePosition).getChildren(), Integer.valueOf(typeNos), typeDatas.get(tabPosition).getProjectscale());
                    }
                    int pos1 = getItemPos(PickCateType.TYPE_CATEGORY_0);
                    int pos2 = getItemPos(PickCateType.TYPE_CATEGORY_1);
                    int pos3 = getItemPos(PickCateType.TYPE_CATEGORY_2);
                    categoryFilterPopWindow.setCateData(pos1, pos2, pos3);
                }
                break;
            case R.id.iv_choose:
                if (!TextUtils.isEmpty(typeNos)) {
                    mFilters.put(PickCateType.TYPE_CATEGORY_0,tabTypePosition);
                    mLeftDialog2.initCategory(Integer.parseInt(typeNos), false,false);
                    mLeftDialog2.initData(mData, mFilters);
                    mLeftDialog2.showDialog();
                }
                break;
            case R.id.survey_view:
                binding.surveyView.setImageResource(R.drawable.btn_shop_up);
                pickTabLayoutPopWindow.showPopwindow(binding.surveyView);
                pickTabLayoutPopWindow.setData(tabTypeList,binding.tabLayoutType.getSelectedTabPosition());
                break;
        }
    }

    private void resetCategoryFilterPopWindow() {
        clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
        getCategoryAllList();
    }

    private void getCategoryAllList() {
        viewModel.getTenderGoodsCategoryAllList(typeNos);
    }

    private PickChooseLeftDialog2 mLeftDialog2;
    // 数据集
    private SparseArray<PickSelectTextEntity> mData = new SparseArray<>();
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> nameArray = new SparseArray<>();

    private void initChooseDialog() {
        nameArray.put(PickCateType.TYPE_CATEGORY_0, "类别");
        nameArray.put(PickCateType.TYPE_CATEGORY_1, "类别1");
        nameArray.put(PickCateType.TYPE_CATEGORY_2, "类别2");
        nameArray.put(PickCateType.TYPE_PICK_MONEY, "中标金额");
        nameArray.put(PickCateType.TYPE_PROVINCE, "省份");
        nameArray.put(PickCateType.TYPE_CITY, "市区");
        mLeftDialog2 = new PickChooseLeftDialog2(getContext(), nameArray);
        mLeftDialog2.setCallBack(this);
    }

    @Override
    public void loadCategory(PickSelectTextEntity entity, int itemPos) {
        List<PickSelectEntity> children = new ArrayList<>();
        if (itemPos > 0) {
            switch (entity.getSort()) {
                case PickCateType.TYPE_CATEGORY_0:
                    if (categoryDatas == null || categoryDatas.size() == 0) {
                        return;
                    }
                    List<PickSelectEntity<List<PickSelectEntity>>> cate1 = categoryDatas.get(itemPos).getChildren();
                    if (cate1 != null && cate1.size() > 0) {
                        children.addAll(cate1);
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickCateType.TYPE_CATEGORY_1, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickCateType.TYPE_CATEGORY_1:
                    if (categoryDatas == null || categoryDatas.size() == 0) {
                        return;
                    }
                    int firPos = mLeftDialog2.getItemPos(0);
                    List<PickSelectEntity> cate2 = categoryDatas.get(firPos).getChildren().get(itemPos).getChildren();
                    if (cate2 != null && cate2.size() > 0) {
                        children.addAll(cate2);
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickCateType.TYPE_CATEGORY_2, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickCateType.TYPE_PROVINCE:
                    String itemNo = entity.getItemNo(itemPos);
                    viewModel.getAddressCityDataDialog(scrData, itemNo);
                    break;
            }
        } else {
            if (entity.getSort() == PickCateType.TYPE_CATEGORY_0 || entity.getSort() == PickCateType.TYPE_CATEGORY_1 || entity.getSort() == PickCateType.TYPE_PROVINCE) {
                mLeftDialog2.clearOtherDataInfo(entity.getSort());
                mLeftDialog2.showDialog();
            }
        }

    }

    @Override
    public void loadData(SparseArray<PickSelectTextEntity> data, SparseIntArray map) {
        mData = PickCopyUtils.deepCopy(data);
        mFilters = map.clone();
        if (mFilters.size() > 0 && mFilters.get(0) > 0) {
            isDialogData = true;
            binding.tabLayoutType.getTabAt(mFilters.get(0)).select();
        }
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            PickSelectTextEntity entity = mData.get(key);
            updateChooseStatus(entity.getSort(), mFilters.get(entity.getSort()));
        }
        getData();
    }

    @Override
    public void reset() {
        mFilters.clear();
        clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
        clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
        loadData(mData, mFilters);
        mLeftDialog2.initData(mData, mFilters);
        mLeftDialog2.showDialog();
        if (binding.tabLayoutType.getTabAt(0) != null) {
            binding.tabLayoutType.getTabAt(0).select();
        }
    }

    @Override
    public void onCancel() {
        mLeftDialog2.initData(mData, mFilters);
    }

    /**
     * 添加数据到中数据聚合
     */
    private void refreshDataInfo(int sort, String name, List<PickSelectEntity> children) {
        clearOtherDataInfo(sort);
        addEntity(sort, children);
    }

    /**
     * 清理关联项为空
     */
    private void clearOtherDataInfo(int sort) {
        switch (sort) {
            case PickCateType.TYPE_CATEGORY_0:
                addEntity(PickCateType.TYPE_CATEGORY_1, new ArrayList<>());
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_CATEGORY_1:
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_PROVINCE:
                addEntity(PickCateType.TYPE_CITY, new ArrayList<>());
                break;
        }
    }

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    /**
     * 获取当前缓存位置下标
     *
     * @param pos index
     */
    public int getItemPos(int pos) {
        return mFilters.get(pos);
    }

    public List<PickSelectEntity> getItemData(int index) {
        if (mData.get(index) != null) {
            return mData.get(index).getPickSelectEntityList();
        }
        return new ArrayList<>();
    }

    /**
     * 筛选赋值
     */
    private void updateChooseStatus(int key, int value) {
        mFilters.put(key, value);
        String no = mData.get(key).getItemNo(value);
        String name = mData.get(key).getItemName(value);
        switch (key) {
            case PickCateType.TYPE_CATEGORY_0:  //类别
                if (TextUtils.isEmpty(no)) {
                    name = "类别";
                }
                if(tabTypeList.size() > 0){
                    goodsType = tabTypeList.get(tabTypePosition).getNo();
                    updateChooseTextStatus(binding.tvCategory, value, name);
                }
                goodsTypeName = name;
                break;
            case PickCateType.TYPE_CATEGORY_1:
            case PickCateType.TYPE_CATEGORY_2:
                if(!TextUtils.isEmpty(no)){
                    goodsType = no;
                    goodsTypeName = name;
                    if (value > 0) {
                        updateChooseTextStatus(binding.tvCategory, value, name);
                    }
                }
                break;
            case PickCateType.TYPE_PICK_MONEY: // 中标金额
                pickMoney = no;
                pickMoneyName = name;
                updateChooseTextStatus(binding.tvPickMoney, value, name);
                break;
            case PickCateType.TYPE_PROVINCE: // 省
                areacode = no;
                mAddressPop.setDefPosition(value, 0);
                if (value == 0) {
                    areaName = "地区";
                } else {
                    areaName = name;
                }
                updateChooseTextStatus(binding.tvArea, value, areaName);
                break;
            case PickCateType.TYPE_CITY: // 城市
                citysNos = no;
                mAddressPop.setDefPosition(mFilters.get(PickCateType.TYPE_PROVINCE), value);
                if (value > 0) {
                    areaName = name;
                    updateChooseTextStatus(binding.tvArea, value, areaName);
                }
                break;
        }
    }

    /**
     * 更新选项颜色
     */
    private void updateChooseTextStatus(PickPopWindowView pickPopWindowView, int value, String name) {
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > 4) {
                name = name.substring(0, 4) + "...";
            }
        }
        if (value == -1 || value == -2) {
            binding.popLine.setVisibility(View.VISIBLE);
            pickPopWindowView.setChooseData(R.drawable.member_pop_blue_bg2, R.mipmap.ic_up_arrow_n, R.color.color_333333,true,name);
        }else if (value == 0) {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333,false,name);
        } else {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_new_blue_bg_10, R.mipmap.icon_down_red, R.color.color_main,false,name);
        }
    }

    /**
     * 分类
     */
    public void setTenderGoodsCategoryAllList(List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> datas) {
        categoryDatas.clear();
        categoryDatas.addAll(datas);
        if (categoryDatas.size() != 0) {
            categoryDatas.get(0).setCheck(true);
            List<PickSelectEntity> list = new ArrayList<>();
            list.addAll(categoryDatas);
            refreshDataInfo(PickCateType.TYPE_CATEGORY_0, "类别", list);

            for (int i = 0; i < categoryDatas.size(); i++) {
                PickSelectEntity pickSelectEntity = new PickSelectEntity(categoryDatas.get(i).getNo(), categoryDatas.get(i).getProjectscale());
                tabTypeList.add(pickSelectEntity);
                binding.tabLayoutType.addTab(binding.tabLayoutType.newTab().setText(pickSelectEntity.getProjectscale()));
            }
        }
    }

    public void setPriceDatas(List<PickSelectEntity> datas) {
        refreshDataInfo(PickCateType.TYPE_PICK_MONEY, "中标金额", datas);
    }

    public void setSortDatas(List<PickSelectEntity> datas) {
        sortData.clear();
        sortData.addAll(datas);
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        typeDatas.clear();
        typeDatas.addAll(datas);
        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeDatas.get(i).getProjectscale()));
        }
    }

    public void setChooseItemData(int type, List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) return;
        switch (type) {
            case PickCateType.TYPE_PROVINCE:
                refreshDataInfo(PickCateType.TYPE_PROVINCE, "省份", data);
                break;
            case PickCateType.TYPE_CITY:
                refreshDataInfo(PickCateType.TYPE_CITY, "市区", data);
                break;
        }
    }

    public void setProvinceDatas(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            mAddressPop.setData(scrData, true);
        } else {
            viewModel.getProvinceData(PickCateType.TYPE_CITY);
        }
    }


    public void setCityDatas(List<PickSelectEntity> datas) {
        mLeftDialog2.addEntity(PickCateType.TYPE_CITY, datas);
        mLeftDialog2.showDialog();
    }

    /**
     * 中标金额
     */
    private void showMoneyPopWindow() {
        if(TextUtils.isEmpty(pickMoneyName)){
            pickMoneyName = "中标金额" ;
        }
        updateChooseTextStatus(binding.tvPickMoney, TextUtils.isEmpty(pickMoney) ? -1 : -2, pickMoneyName);
        pickMoneyPopWindow.showPopwindow0(binding.tvPickMoney);
        int value = getItemPos(PickCateType.TYPE_PICK_MONEY);
        pickMoneyPopWindow.setData(getItemData(PickCateType.TYPE_PICK_MONEY), value);
    }


    /**
     * 省份
     */
    private void showProvincePopWindow() {
        if (scrData == null) {
            viewModel.getProvinceData(PickCateType.TYPE_PROVINCE);
            return;
        }
        if (TextUtils.isEmpty(areaName)) {
            areaName = "地区";
        }
        updateChooseTextStatus(binding.tvArea, TextUtils.isEmpty(areacode) && TextUtils.isEmpty(citysNos) ? -1 : -2, areaName);
        mAddressPop.showPopwindow0(binding.tvArea);
    }


    /**
     * 排序
     */
    private void showSortPopWindow() {
        updateChooseTextStatus(binding.tvSort, Integer.valueOf(sortType) == 1 ? -1 : -2, null);
        pickSortPopWindow.showPopwindow0(binding.tvSort);
        pickSortPopWindow.setData(sortData, false);
    }

    public void fullScroll(){
        binding.noticeList.getLayoutManager().scrollToPosition(0);
    }

    private void initData() {
        page = 1;
        getData();
        binding.noticeList.setVisibility(View.VISIBLE);
        binding.empty.setVisibility(View.VISIBLE);

        binding.tvArea.setOnClickListener(this);
        binding.tvCategory.setOnClickListener(this);
        binding.tvPickMoney.setOnClickListener(this);
        binding.tvSort.setOnClickListener(this);
        binding.ivChoose.setOnClickListener(this);
        binding.surveyView.setOnClickListener(this);

        categoryFilterPopWindow = new CommonCategoryFilterPopWindow(getActivity(),false);
        categoryFilterPopWindow.setBgView(binding.grayLayout);
        categoryFilterPopWindow.setResultListener(new CommonCategoryFilterPopWindow.Listener() {
            @Override
            public void setResultListener(String text, String result, String typeNo) {
            }

            @Override
            public void setResetResultListener() {
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
            }
        });
        categoryFilterPopWindow.setCategoryCallBack(new CommonCategoryFilterPopWindow.CategoryCallBack() {
            @Override
            public void resultPosition(String posName1, String posName2, String posName3, int pos1, int pos2, int pos3) {
                clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
                pos1 = tabTypePosition;
                mFilters.put(PickCateType.TYPE_CATEGORY_0,pos1);
                if (pos1 > 0) {
                    if (pos2 > 0) {
                        List<PickSelectEntity> list2 = new ArrayList<>();
                        List<PickSelectEntity<List<PickSelectEntity>>> cate1 = categoryDatas.get(pos1).getChildren();
                        if (cate1 != null && cate1.size() > 0) {
                            list2.addAll(cate1);
                        }
                        refreshDataInfo(PickCateType.TYPE_CATEGORY_1, "", list2);
                        updateChooseStatus(PickCateType.TYPE_CATEGORY_1, pos2);
                        if (pos3 > 0) {
                            List<PickSelectEntity> list3 = new ArrayList<>();
                            List<PickSelectEntity> cate2 = categoryDatas.get(pos1).getChildren().get(pos2).getChildren();
                            if (cate2 != null && cate2.size() > 0) {
                                list3.addAll(cate2);
                            }
                            refreshDataInfo(PickCateType.TYPE_CATEGORY_2, "", list3);
                            updateChooseStatus(PickCateType.TYPE_CATEGORY_2, pos3);
                        }
                    }else{
                        //重置
                        updateChooseStatus(PickCateType.TYPE_CATEGORY_0, 0);
                    }
                }
                getData();
            }
        });
        categoryFilterPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int pos1 = getItemPos(PickCateType.TYPE_CATEGORY_0);
                updateChooseTextStatus(binding.tvCategory, pos1 > 0 ? 1 : 0, goodsTypeName);
            }
        });

        // 金额
        pickMoneyPopWindow = new PickHallBidingPopWindow(getActivity());
        pickMoneyPopWindow.setBgView(binding.grayLayout);
        pickMoneyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                updateChooseStatus(PickCateType.TYPE_PICK_MONEY, (Integer) key3);
                getData();
            }
        });
        pickMoneyPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickCateType.TYPE_PICK_MONEY);
                updateChooseTextStatus(binding.tvPickMoney, value > 0 ? 1 : 0, pickMoneyName);
            }
        });
        pickMoneyPopWindow.setDefItem();
        //排序方式
        pickSortPopWindow = new PickHallBidingPopWindow(getActivity());
        pickSortPopWindow.setBgView(binding.grayLayout);
        pickSortPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                sortTypeName = ((PickSelectEntity) key1).getProjectscale();
                sortType = ((PickSelectEntity) key1).getNo();
                updateChooseTextStatus(binding.tvSort, (Integer) key3, sortTypeName);
                getData();
            }
        });
        pickSortPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.tvSort, Integer.valueOf(sortType) == 1  ? 0 : 1, sortTypeName);
            }
        });

        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabPosition = tab.getPosition();
                typeNos = typeDatas.get(tabPosition).getNo();
                if(tabPosition == 0 || tabPosition == (typeDatas.size() - 1)){
                    binding.tvCategory.setVisibility(View.GONE);
                    binding.tvArea.setVisibility(View.VISIBLE);
                    if (mData != null && mData.size() > 0 && mData.get(0) != null) {
                        if (mData.get(0).getTypeName().contains("类别")) {
                            mData.remove(0);
                        }
                    }
                }else{
                    binding.tvCategory.setVisibility(View.VISIBLE);
                    binding.tvArea.setVisibility(View.VISIBLE);
                    resetCategoryFilterPopWindow();
                }
//                sortType = "1";
//                updateChooseTextStatus(binding.tvSort, Integer.valueOf(sortType) == 1  ? 0 : 1, "综合排序");
//                pickSortPopWindow.setDefItem();
//                mFilters.clear();
                clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
                clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
                loadData(mData, mFilters);
                if(tabPosition == 0 || tabPosition == (typeDatas.size() - 1)){
                    binding.lineTabType.setVisibility(View.GONE);
                }else{
                    tabTypeList.clear();
                    binding.tabLayoutType.removeAllTabs();
                    binding.lineTabType.setVisibility(View.VISIBLE);
                    binding.tvCategory.setVisibility(View.GONE);
                    mFilters.put(PickCateType.TYPE_CATEGORY_0, 0);
                    mFilters.put(PickCateType.TYPE_CATEGORY_1, 0);
                    mFilters.put(PickCateType.TYPE_CATEGORY_2, 0);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tabLayoutType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabTypePosition = tab.getPosition();
                if(tabTypePosition == 0){
                    binding.tvCategory.setVisibility(View.GONE);
                }else{
                    List<PickSelectEntity<List<PickSelectEntity>>> pickSelectEntities = categoryDatas.get(tabTypePosition).getChildren();
                    if (pickSelectEntities == null || pickSelectEntities.size() == 0) {
                        binding.tvCategory.setVisibility(View.GONE);
                    } else {
                        binding.tvCategory.setVisibility(View.VISIBLE);
                        if(!isDialogData){
                            updateChooseStatus(PickCateType.TYPE_CATEGORY_0, 0);
                            mFilters.put(PickCateType.TYPE_CATEGORY_1, 0);
                            mFilters.put(PickCateType.TYPE_CATEGORY_2, 0);
                        }
                    }
                }
                pickTabLayoutPopWindow.setDefItem(tabTypePosition);
                goodsType = tabTypeList.get(tabTypePosition).getNo();
                isDialogData = false;
                getData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        pickTabLayoutPopWindow = new PickHallBidingPopWindow(getActivity());
        pickTabLayoutPopWindow.setBgView(binding.grayLayout);
        pickTabLayoutPopWindow.setOnResultClick((key1, key2, key3) -> {
            binding.tabLayoutType.getTabAt((int) key3).select();
            pickTabLayoutPopWindow.setDefItem((int) key3);
            goodsType = ((PickSelectEntity) key1).getNo();
            getData();
        });
        pickTabLayoutPopWindow.setOnDisMissClick(() -> {
            binding.surveyView.setImageResource(R.drawable.btn_down);
        });

        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");

        viewModel.getProvinceData(PickCateType.TYPE_PROVINCE);
        viewModel.getFilterTypeData();
        getData();
        viewModel.getSortTypeData();
        viewModel.getSelecteScaleData();

        initAddressPopup();
    }

    private void getData(){
        viewModel.findBidNoticePage(searchName,total,page,typeNos,pickMoney,areacode,citysNos,sortType,goodsType);
    }
    
    private void initAddressPopup() {
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setBgView(binding.grayLayout);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {

                clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    areaName = entity.getProvinceName();
                    areacode = entity.getProvinceNo();
                    citysNos = "";
                    if ("0".equals(areacode)) {
                        areaName = "地区";
                    }
                    updateChooseTextStatus(binding.tvArea, 1, areaName);
                    mFilters.put(PickCateType.TYPE_PROVINCE, mAddressPop.getProvincePosition());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        citysNos = entity.getCityNo();
                        areaName = entity.getCityName();
                        viewModel.getAddressCityData(scrData, entity.getProvinceNo(), PickCateType.TYPE_CITY);
                        updateChooseTextStatus(binding.tvArea, 1, areaName);
                        mFilters.put(PickCateType.TYPE_CITY, mAddressPop.getmCityPosition());
                    }
                }else{
                    mFilters.put(PickCateType.TYPE_PROVINCE, 0);  //重置
                    mFilters.put(PickCateType.TYPE_CITY, 0);
                    areaName = "地区";
                    areacode = "";
                    citysNos = "";
                    updateChooseTextStatus(binding.tvArea, 0, areaName);
                }
                getData();
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            int pos1 = getItemPos(PickCateType.TYPE_PROVINCE);
            updateChooseTextStatus(binding.tvArea, pos1 > 0 ? 1 : 0, areaName);
        });
    }

    public void getBidedData(List<PreNoticeItemEntity> list) {
        if (isMore) {
            adapter.addDatas(list);
            binding.noticeList.loadMoreComplete();
        } else {
            adapter.setDatas(list);
        }
        binding.noticeList.refreshComplete();
        binding.noticeList.setVisibility(View.VISIBLE);
        binding.empty.setVisibility(View.GONE);
    }

    public void empty() {
        if (null != adapter.getDatas() && adapter.getDatas().size() > 0) {
            binding.noticeList.loadMoreComplete();
        }
        binding.noticeList.setVisibility(View.GONE);
        binding.empty.setVisibility(View.VISIBLE);
    }
}

